
/*
 * asm_rutines.s
 *
 * Created: 17.7.2013 19:32:06
 *  Author: Elektronik
 */ 

 #include <avr/io.h>
 #include <avr/interrupt.h>

 		
/****************************************************
 * Enable watchdog
 *\param R24 prescaller value
 *****************************************************/

 .section .text
.global EnableWDT

EnableWDT:
		in		r20, _SFR_IO_ADDR(SREG)						; store SREG
		cli													; disable interrupts

		wdr													; reset wdr
		ldi		r18, (1 << WDCE) | (1 << WDE)				; enable change 
		ori		r24, (1 << WDE)								; or enable wdt bit
		sts		WDTCSR, r18
		sts		WDTCSR, r24
				
		out		_SFR_IO_ADDR(SREG), r20						; restore SREG
		ret


/****************************************************
 * Disable watchdog
 *****************************************************/

 .section .text
 .global DisableWDT

 DisableWDT:
		in		r19, _SFR_IO_ADDR(SREG)			; store SREG
		cli										; disable interrupts	

		wdr
		in		r18, _SFR_IO_ADDR(MCUSR)		; load MCUSR
		andi	r18, ~(1 << WDRF)				; clear WDRF == > enable clear WDE
		out		_SFR_IO_ADDR(MCUSR), r18
			
		ldi		r18, (1 << WDCE) | (1 << WDE)	; enable change and enable set WDCE and WDE
		sts		WDTCSR, r18
		sts		WDTCSR, r1						; disable wdt

		out		_SFR_IO_ADDR(SREG), r19			; restore SREG
		ret

 /****************************************************
 * Jump to specified address
 * 
 *****************************************************/
 
 .section .text
 .global JumpToAddress

JumpToAddress:
		movw	r30, r24			; low word to Z
		ijmp						; jump



/*****************************************************
* Return length of string
*
* R25:R24 - pointer to string
* return R24 - size of string
* 
* change R31:R30
*******************************************************/

/*
.section .text
.global strlen

strlen:
		movw	r30, r24				; move address to Z reg
		clr		r24						; clear counter

strlen_loop:	
		ld		r25, Z+					; load char
		tst		r25						; test for zero
		breq	strlen_end				; char is 0, go to end
		inc		r24						; increment counter
		rjmp	strlen_loop				; do next loop
strlen_end:
		ret
*/


.end